//
//  XNPurchaseViewController.m
//  XINNingOA
//
//  Created by samuel on 2021/7/14.
//

#import "XNPurchaseViewController.h"
#import "XNPurchaseBillInfoModel.h"
#import "XNPurchaseApproveRequest.h"
#import "XNPurchaseBillInfoRequest.h"

@interface XNPurchaseViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) XNPurchaseBillInfoModel *infoModel;


@end

@implementation XNPurchaseViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [SVProgressHUD show];
    __weak typeof(self)weakSelf = self;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group);
    XNPurchaseBillInfoRequest *infoRequest = [[XNPurchaseBillInfoRequest alloc] initWithOrderId:self.orderId];
    [infoRequest startWithCompletionBlockWithSuccess:^(__kindof XNPurchaseBillInfoRequest * _Nonnull request) {
        weakSelf.infoModel = request.infoModel;
        dispatch_group_leave(group);
    } failure:^(__kindof XNPurchaseBillInfoRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_enter(group);
    XNPurchaseApproveRequest *approveRequest = [[XNPurchaseApproveRequest alloc] initWithOrderId:self.orderId];
    [approveRequest startWithCompletionBlockWithSuccess:^(__kindof XNPurchaseApproveRequest * _Nonnull request) {
        weakSelf.approveArray = request.approveFlowArray;
        dispatch_group_leave(group);
    } failure:^(__kindof XNPurchaseApproveRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_notify(group, queue, ^{
        [weakSelf creatTableData];
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            [weakSelf.tableView reloadData];
        });

    });
    
}

- (void)creatTableData
{
    
    XNTableShowDataModel *infoModel = [[XNTableShowDataModel alloc] init];
    infoModel.showType = XNShowType_Normal;
    infoModel.title = @"采购单信息";
    XNShowItemModel *itemModel = [[XNShowItemModel alloc] init];
    NSArray *showItemArray = [NSArray arrayWithObjects:
                              [NSString stringWithFormat:@"单据编号： %@",self.infoModel.ID],
                              [NSString stringWithFormat:@"制单日期： %@",self.infoModel.createTimeStr],
                              [NSString stringWithFormat:@"制单人： %@",self.infoModel.createUserName],
                              [NSString stringWithFormat:@"制单部门： %@",self.infoModel.createUserObject.departmentName],
                              [NSString stringWithFormat:@"采购责任人： %@",self.infoModel.purchaserName],
                              [NSString stringWithFormat:@"采购部门： %@",self.infoModel.departmentName],
                              [NSString stringWithFormat:@"供应商： %@",self.infoModel.partnerName],
                              [NSString stringWithFormat:@"备注： %@",self.infoModel.remark.length > 0 ? self.infoModel.remark : @""],
                              [NSString stringWithFormat:@"询价记录： %@",self.infoModel.enquiryInfo.length > 0 ? self.infoModel.enquiryInfo : @""],
                              nil];
    itemModel.showItemArray = showItemArray;
    [infoModel.showDataArray addObject:itemModel];
    [self.showArray addObject:infoModel];
    
    XNTableShowDataModel *submitApplyModel = [[XNTableShowDataModel alloc] init];
    submitApplyModel.showType = XNShowType_Normal;
    submitApplyModel.title = @"采购明细信息";
    [self.infoModel.itemList enumerateObjectsUsingBlock:^(XNPurchaseItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *temp = [[XNShowItemModel alloc] init];
        NSArray *showItemArray4 = [NSArray arrayWithObjects:
                                   [NSString stringWithFormat:@"物资名称： %@",obj.publicResourceItemName],
                                   [NSString stringWithFormat:@"物资类别： %@",obj.publicResourceName.length > 0 ? obj.publicResourceName: @""],
                                   [NSString stringWithFormat:@"规格型号： %@",obj.brand.length > 0 ? obj.brand: @""],
                                   [NSString stringWithFormat:@"尺寸： %@",obj.size.length > 0 ? obj.size: @""],
                                   [NSString stringWithFormat:@"单位： %@",obj.unit],
                                   [NSString stringWithFormat:@"采购数量： %ld",obj.num],
                                   [NSString stringWithFormat:@"单价： %.3f",obj.price],
                                   nil];
        temp.showItemArray = showItemArray4;
        [submitApplyModel.showDataArray addObject:temp];
    }];
    [self.showArray addObject:submitApplyModel];
    
    
    
    
    XNTableShowDataModel *approveModel = [[XNTableShowDataModel alloc] init];
    approveModel.showType = XNShowType_Approve;
    approveModel.title = @"审批流程";
    [self.approveArray enumerateObjectsUsingBlock:^(XNBillApproveModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *temp = [[XNShowItemModel alloc] init];
        temp.showItemArray = [NSArray arrayWithObject:obj];
        [approveModel.showDataArray addObject:temp];
    }];
    [self.showArray addObject:approveModel];
    
}

@end
